import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:kiss/view/note_index.dart';
import 'package:timeago/timeago.dart' as timeago;

import 'model/category.dart';
import 'model/note.dart';


void main() async {
  await Hive.initFlutter();
  Hive.registerAdapter(NoteAdapter());
  Hive.registerAdapter(CategoryAdapter());
  await Hive.openBox<Note>('notes');
  await Hive.openBox<Category>('categories');
  timeago.setLocaleMessages('zh_CN', timeago.ZhCnMessages());
  runApp(GetMaterialApp(home: HomeScreen(),debugShowCheckedModeBanner: false,debugShowMaterialGrid: false,));
}

class HomeController extends GetxController{
  // 当前选中的底部导航栏索引
  var currentIndex = 0.obs;

  // PageView 控制器
  late PageController pageController;

  @override
  void onInit() {
    super.onInit();
    pageController = PageController();
  }

  @override
  void onClose() {
    pageController.dispose();
    super.onClose();
  }

  // 切换页面
  void changePage(int index) {
    currentIndex.value = index;
    pageController.jumpToPage(index);
  }
}


class HomeScreen extends StatelessWidget {
  final HomeController controller = Get.put(HomeController());

  final List<Widget> pages = [
    NoteIndexView(),
    BookIndexPage(),
    SettingIndexPage(),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        controller: controller.pageController,
        onPageChanged: (index) {
          controller.currentIndex.value = index;
        },
        children: pages,
      ),
      bottomNavigationBar: Obx(
            () => BottomNavigationBar(
          currentIndex: controller.currentIndex.value,
          onTap: (index) {
            controller.changePage(index);
          },
          items: [
            BottomNavigationBarItem(
              icon: Icon(Icons.note_alt_outlined),
              label: '小记',
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.book_outlined),
              label: '书架',
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.settings),
              label: '设置',
            ),
          ],
        ),
      ),
    );
  }
}

// 小记主页
// class NoteIndexPage extends StatelessWidget {
//   const NoteIndexPage({super.key});
//
//   @override
//   Widget build(BuildContext context) {
//     return Center(
//       child: Text('小记主页'),
//     );
//   }
// }

// 书籍主页
class BookIndexPage extends StatelessWidget {
  const BookIndexPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text('书籍主页'),
    );
  }
}

// 设置主页
class SettingIndexPage extends StatelessWidget {
  const SettingIndexPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text('设置主页'),
    );
  }
}





